package com.chuangke.metadata.controller;

import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.chuangke.common.http.HttpResult;
import com.chuangke.metadata.domain.GridModelCopyDto;
import com.chuangke.metadata.service.SysGridService;

/**
 * @author chuangke
 * @date 2023-01-12
 */
@RestController
@RequestMapping("grid/model")
public class SysGridController {

	@Autowired
	private SysGridService sysGridService;

	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/copy")
	public HttpResult page(@RequestBody GridModelCopyDto cmcd) {
		sysGridService.copy(cmcd.getTargetModel(), cmcd.getSourceModelId()) ;
		return HttpResult.ok();
	}

	@PreAuthorize("hasAuthority('login:action')")
	@GetMapping(value = "/init")
	public HttpResult init(@RequestParam String dataBase,@RequestParam String tableName,@RequestParam String moudleId) {
		sysGridService.init(dataBase,tableName,moudleId,null,null);
		return HttpResult.ok();
	}
	
	@PreAuthorize("hasAuthority('login:action')")
	@PostMapping(value = "/init")
	public HttpResult initPost(@RequestBody Map<String,String> params) {
		String dataBase = params.get("dataBase") ; 
		String tableName=params.get("tableName") ; 
		String moudleId =params.get("moudleId") ;
		String modelId=params.get("modelId") ; 
		String modelName =params.get("modelName") ;
		
		sysGridService.init(dataBase,tableName,moudleId,modelId,modelName);
		return HttpResult.ok();
	}
}
